package cn.tianyigps.tianyigpsapiv2.oauth2.context;

import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;

import java.security.Principal;

/**
 * Created with IDEA
 * author:guibin.cui
 * Date:2018/12/18
 * Time:10:42
 * 获取登陆信息
 */

public class ClientPrincipal {

    public static String getClientId(){
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (principal instanceof UserDetails) {
            return ((UserDetails) principal).getUsername();
        }
        if (principal instanceof Principal) {
            return ((Principal) principal).getName();
        }
        return String.valueOf(principal);
    }
}
